package com.v2coding.mybatis.enmus;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.v2coding.validate.BaseEnum;

import java.io.IOException;

/**
 * EnumSerializer : 将BaseEnum接口的枚举对象序列化为JSON对象
 * ---------------------------------------------------------------------------------------------------------------------
 */
public class EnumSerializer extends JsonSerializer<BaseEnum> {
    public EnumSerializer() {
    }

    public void serialize(BaseEnum value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        if (null != value) {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeFieldName("label");
            jsonGenerator.writeString(value.getLabel());
            jsonGenerator.writeFieldName("value");
            jsonGenerator.writeObject(value.getValue());
            jsonGenerator.writeEndObject();
        } else {
            jsonGenerator.writeNull();
        }

    }
}
